package com.yzjs.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import org.simpleframework.xml.Element;

public class MapUtil {
    static final ObjectMapper objectMapper = new ObjectMapper();

    public MapUtil() {
    }

    public static Map<String, String> buildMap(Object obj) {
        HashMap map = new HashMap();

        try {
            Class<?> clazz = obj.getClass();
            Field[] var3 = clazz.getDeclaredFields();
            int var4 = var3.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                field.setAccessible(true);
                String fieldName = field.getName();
                Element element = (Element)field.getAnnotation(Element.class);
                if (element != null && StringUtils.isNotEmpty(element.name())) {
                    fieldName = element.name();
                }

                String value = field.get(obj) == null ? "" : String.valueOf(field.get(obj));
                map.put(fieldName, value);
            }
        } catch (Exception var10) {
            var10.printStackTrace();
        }

        return map;
    }

    public static Map<String, String> getMap(String mapStr) {
        if (StringUtils.isEmpty(mapStr)) {
            return null;
        } else {
            Map<String, String> map = new HashMap();
            String[] key2ValArr = mapStr.replace("{", "").replace("}", "").split(", ");

            for(int i = 0; i < key2ValArr.length; ++i) {
                String[] keyAndVal = key2ValArr[i].split("=");
                map.put(keyAndVal[0], keyAndVal[1]);
            }

            return map;
        }
    }

    public static String toUrl(Map<String, String> map) {
        String url = "";

        Entry entry;
        for(Iterator var2 = map.entrySet().iterator(); var2.hasNext(); url = url + (String)entry.getKey() + "=" + (String)entry.getValue() + "&") {
            entry = (Entry)var2.next();
        }

        url = StringUtils.substringBeforeLast(url, "&");
        return url;
    }

    public static String toUrlWithSort(Map<String, String> map) {
        List<String> keys = new ArrayList(map.keySet());
        Collections.sort(keys);
        String prestr = "";

        for(int i = 0; i < keys.size(); ++i) {
            String key = (String)keys.get(i);
            String value = (String)map.get(key);
            if (i == keys.size() - 1) {
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }

        return prestr;
    }

    public static Map<String, String> removeParamsForAlipaySign(Map<String, String> map) {
        map.remove("sign");
        map.remove("sign_type");
        return map;
    }

    public static Map<String, String> removeEmptyKeyAndValue(Map<String, String> map) {
        Iterator it = map.entrySet().iterator();

        while(true) {
            String key;
            String value;
            do {
                if (!it.hasNext()) {
                    return map;
                }

                Entry<String, String> entry = (Entry)it.next();
                key = (String)entry.getKey();
                value = (String)entry.getValue();
            } while(!StringUtils.isBlank(key) && !StringUtils.isBlank(value));

            it.remove();
        }
    }

    public static Map<String, String> keyToLowerCase(Map<String, String> map) {
        Map<String, String> responseMap = new HashMap();
        Iterator it = map.entrySet().iterator();

        while(it.hasNext()) {
            Entry<String, String> entry = (Entry)it.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            responseMap.put(key.toLowerCase(), value);
        }

        return responseMap;
    }

    public static String toUrlWithSortAndEncode(Map<String, String> map) {
        List<String> keys = new ArrayList(map.keySet());
        Collections.sort(keys);
        String prestr = "";

        for(int i = 0; i < keys.size(); ++i) {
            String key = (String)keys.get(i);
            String value = (String)map.get(key);
            if (value == null) {
                break;
            }

            if (i == keys.size() - 1) {
                prestr = prestr + key + "=" + URLEncoder.encode(value);
            } else {
                prestr = prestr + key + "=" + URLEncoder.encode(value) + "&";
            }
        }

        return prestr;
    }

    public static HashMap<String, String> form2Map(String orderinfo) {
        HashMap<String, String> map = new HashMap();
        String[] listinfo = orderinfo.split("&");
        String[] var3 = listinfo;
        int var4 = listinfo.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String s = var3[var5];
            String[] list = s.split("=");
            if (list.length > 1) {
                map.put(list[0], list[1]);
            }
        }

        return map;
    }

    public static HashMap<String, String> form2MapWithCamelCase(String orderinfo) {
        HashMap<String, String> map = new HashMap();
        String[] listinfo = orderinfo.split("&");
        String[] var3 = listinfo;
        int var4 = listinfo.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String s = var3[var5];
            String[] list = s.split("=");
            if (list.length > 1) {
                map.put(CamelCaseUtil.toCamelCase(list[0]), list[1]);
            }
        }

        return map;
    }

    public static Map<String, String> object2MapWithUnderline(Object obj) {
        HashMap map = new HashMap();

        try {
            Class<?> clazz = obj.getClass();
            Field[] var3 = clazz.getDeclaredFields();
            int var4 = var3.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                field.setAccessible(true);
                String fieldName = field.getName();
                fieldName = CamelCaseUtil.toUnderlineName(fieldName);
                String value = field.get(obj) == null ? "" : String.valueOf(field.get(obj));
                map.put(fieldName, value);
            }
        } catch (Exception var9) {
            var9.printStackTrace();
        }

        return map;
    }

    public static <T> T mapToObject(Object obj, Class<T> clazz) {
        try {
            return objectMapper.readValue(serialize(obj), clazz);
        } catch (IOException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static String serialize(Object obj) {
        if (obj == null) {
            return null;
        } else if (obj.getClass() == String.class) {
            return (String)obj;
        } else {
            try {
                return objectMapper.writeValueAsString(obj);
            } catch (JsonProcessingException var2) {
                return null;
            }
        }
    }

    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}

